package net.begincode.servlet.project;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import utils.BQStringUtil;
import utils.ViewCount;

import com.google.gson.Gson;

import net.begincode.bean.BcUser;
import net.begincode.bean.ProjectStage;
import net.begincode.servlet.common.BaseServlet;
import net.begincode.servlet.common.ViewServlet;

public class ProjectServlet extends BaseServlet{
	private static final Log LOG = LogFactory.getLog(ProjectServlet.class);
	
	/**
	 * 新增项目阶段初始化
	 * @throws IOException 
	 * @throws ServletException 
	 * */
	public void addProjectStageInit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		request.setAttribute("projects", this.projectService.selProject());	
		request.getRequestDispatcher("/WEB-INF/manage/editProjectState.jsp").forward(request, response);
	}
	/**
	 * 项目阶段保存
	 * */
	public void saveProjectStage(HttpServletRequest request, HttpServletResponse response){
		int returnValue = 0;
		ProjectStage projectStage = new ProjectStage();
		//设置表单参数
		projectStage.setBc_project_id(Integer.valueOf(request.getParameter("projectId")));
		projectStage.setStage_name(request.getParameter("stage_name"));
		projectStage.setStage_content(request.getParameter("stage_content"));
		projectStage.setStage_abstract(request.getParameter("stage_abstract"));
		//设置普通信息
		projectStage.setCheck_flag("1");
		projectStage.setPraise(0);
		projectStage.setView_count(0);
		String proStageId = request.getParameter("proStageId");
		if(proStageId.equals("")){
			returnValue = this.projectStageService.saveProjectStage(projectStage);
		}else{
			projectStage.setProject_stage_id(Integer.parseInt(proStageId));
			returnValue = this.projectStageService.updProStage(projectStage);
		}
		if(returnValue > 0){
			request.setAttribute("msg", "保存成功");
		}else{
			request.setAttribute("projectStage", projectStage);
			request.setAttribute("msg", "保存失败");
		}
		try {
			request.setAttribute("projects", this.projectService.selProject());	
			request.getRequestDispatcher("/WEB-INF/manage/editProjectState.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 查询项目各阶段内容
	 * @throws IOException 
	 * @throws ServletException 
	 * */
	public void viewProject(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		String projectId = (String)request.getAttribute("id");
		request.setAttribute("proStages", this.projectStageService.selProStageByProjectCache(projectId));
		request.setAttribute("project", this.projectService.selProByIdCache(projectId));
		request.getRequestDispatcher("/WEB-INF/page/project_view.jsp").forward(request, response);
	}
	/**
	 * 查看项目阶段
	 * @throws IOException 
	 * @throws ServletException 
	 * */
	public void viewProStage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		String proStageId = (String)request.getAttribute("id");
		ViewCount.addViewCount(ViewServlet.VIEW_PROJSTAGE+":"+proStageId); //添加阅读数
		ProjectStage proStage = this.projectStageService.selProStageByIdCache(proStageId);
		request.setAttribute("proStage", proStage);
		request.setAttribute("project", this.projectService.selProByIdCache(String.valueOf(proStage.getBc_project_id())));
		request.setAttribute("relProStage", this.projectStageService.selProStageByParamCache(proStage.getBc_project_id(), Integer.valueOf(proStageId), 10));
		request.getRequestDispatcher("/WEB-INF/page/project_stage_view.jsp").forward(request, response);
	}
	
	/**
	 * 查看所有项目阶段
	 * @throws IOException 
	 * @throws ServletException 
	 * */
	public void selProStage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		request.setAttribute("proStages", projectStageService.selProStageByParam(new ProjectStage()));
		request.getRequestDispatcher("/WEB-INF/manage/projectStageManage.jsp").forward(request, response);
	}
	
	/**
	 * 项目阶段修改初始化
	 * @throws IOException 
	 * @throws ServletException 
	 * */
	public void  updProStageInit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		String proStageId = request.getParameter("proStageId");
		request.setAttribute("proStage", projectStageService.selProStageById(proStageId));
		request.setAttribute("projects", this.projectService.selProject());	
		request.getRequestDispatcher("/WEB-INF/manage/editProjectState.jsp").forward(request, response);
	}
}
